package com.mxprojects.gui
{
	import com.mxprojects.constructors.SimpleTextField;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.*;
	
	import flash.events.TextEvent;
	
	public class BreadCrumbs
	{
		private static var breadCrumb:SimpleTextField;
		
		public function BreadCrumbs(){}
		
		public static function setup():void {
			//just sets up the text block to hold the breadcrumb data
			breadCrumb = new SimpleTextField("");
			breadCrumb.x = Styles.css.getStyle(".breadCrumbBase").x;
			breadCrumb.y = Styles.css.getStyle(".breadCrumbBase").y;
			MXP3.header.addChild(breadCrumb);
		}
		
		//static function to change the breadcrumbs in the header
		public static function createCrumbs(forumID:int = 0, forumTitle:String = "", threadID:int = 0, threadTopic:String = "", extraWord:String = "", extraDisplay:String = ""):void {
			//clear the current crumbs
			clearCrumbs();
			
			//build the crumbs
			var homeText:String = "<a class=\"breadCrumbLink\" href=\"event:"+Word.FORUMS+"\">" + Language.main(Word.HOME) + "</a>";
			var threadText:String = " " + Config.BREADCRUMB_SEPARATOR + " " + "<a class=\"breadCrumbLink\" href=\"event:"+Word.THREADS+","+ forumID +"\">" + forumTitle + "</a>";
			var postText:String = " " + Config.BREADCRUMB_SEPARATOR + " " + "<a class=\"breadCrumbLink\" href=\"event:"+Word.POSTS+","+ threadID +"\">" + threadTopic + "</a>";
			var extraText:String = " " + Config.BREADCRUMB_SEPARATOR + " " + "<a class=\"breadCrumbLink\" href=\"event:"+extraWord+"\">" + extraDisplay + "</a>";
			
			//setup the breadcrumb text
			breadCrumb.styleSheet = Styles.css;
			breadCrumb.htmlText = "<span class=\"breadCrumbBase\">" + homeText;
			
			if(forumTitle != ""){
				breadCrumb.htmlText += threadText;
			}
			
			if(threadTopic != ""){
				breadCrumb.htmlText += postText;
			}
			
			if(extraWord != ""){
				breadCrumb.htmlText += extraText;
			}
			
			//finish off the style
			breadCrumb.htmlText += "</span>";
			
			//listen for clicks
			breadCrumb.addEventListener(TextEvent.LINK, parseLink, false, 0, true);
		}
		
		public static function clearCrumbs():void {
			if(breadCrumb != null){
				breadCrumb.removeEventListener(TextEvent.LINK, parseLink);
				breadCrumb.htmlText = "";
			}
		}
		
		private static function parseLink(event:TextEvent):void {
			//disect what is clicked
			// [0] is the location
			// [1] will be the ID (if applicable)
			var chunks:Array = event.text.split(",");
			
			//go there where we need to go
			if(chunks[0] == DeepLinking.CUSTOM_LINK){
				DeepLinking.setLink(DeepLinking.CUSTOM_LINK, new Array(chunks[1], chunks[2]));
			}else{
				DeepLinking.setLink(chunks[0], DeepLinking.createLinkArray(chunks[1]));
			}
		}

	}
}